<?php

class Forum_Helper_Pager
{
    /**
     * Singleton instance
     * @var Forum_Helper_Pager
     */
    private static $_instance = null;
    
    /**
     * Retrieve instance of Forum_Helper_Pager
     * 
     * @return Forum_Helper_Pager
     */
    public static function getInstance()
    {
        if (null === self::$_instance) {
            self::$_instance = new self();
        }
        return self::$_instance;
    }
    
    public function show($page, $total, $resOnPage, $url)
    {
        $totalPage = (($total-($total%$resOnPage))/$resOnPage)+($total%$resOnPage>0?1:0);
        $result = "";
        if ($totalPage > 1) {
            $result .= '<ul class="paging no-bullet no-indent"><li>';
            if ($page == 1) {
                $result .= '<span class="previous">&#8592;</span>';
            } else {
                $result .= '<a href="' . $url . '&page=' . ($page - 1) . '" class="previous">&#8592;</a>';
            }
            $result .= '</li>';
             
            for ($i = 1; $i <= $totalPage; $i++) {
                 $result .= '<li>';
                 if ($page == $i) {
                     $result .= '<span class="current">'. $i . '</span>';
                 } else {
                     $result .= '<a href="' . $url . '&page=' . $i . '">' . $i . '</a>';
                 }
                 $result .= '</li>';
            }
            
            $result .= '<li>'; 
            if ($page == $totalPage) {
                $result .= '<span class="next">&#8594;</span>';
            } else {
                $result .= '<a href="' . $url . '&page=' . ($page + 1) . '" class="next">&#8594;</a>';
            }
            $result .= '</li></ul>';
        }
        echo $result;
    }

}